<?php

namespace App\Models;

use App\Helpers\FileTool;
use App\Helpers\Parsedown;
use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Overtrue\LaravelFollow\Traits\CanBeLiked;

class DocArticle extends Model
{
    use SoftDeletes, CanBeLiked;

    protected $table = 'doc_articles';

    protected $guarded = [];


    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function doc()
    {
        return $this->belongsTo(Doc::class, 'doc_id')->withAnyStatus();
    }

    public static function getOrder($doc_id)
    {
        $lastPage = self::query()->where('doc_id', $doc_id)->orderByDesc('order')->first(['order']);

        $order = $lastPage->order ?? 0;
        return $order + 1;

    }

    public function listened()
    {
        return $this->morphMany(UserListened::class, 'item');
    }

    public static function getText($article)
    {
        try {
            $content = json_decode($article->content);
            $text = collect($content)->map(function ($item) {
                return empty($item->text) ? "" : $item->text;
            })->all();
            return implode('', $text);
        } catch (\Exception $exception) {
            return "";
        }

    }

    public static function getContent($article)
    {
        $content_node = json_decode($article->content);
        return collect($content_node)->map(function ($item) {
            $item->text = (string)$item->text;
            return $item;
        })->all();
    }

    public static function getNodeContent($article)
    {
        $content = self::getContent($article);
        $Parsedown = new Parsedown();
        $nodes = collect($content)->map(function ($item) use ($Parsedown) {
            if ($item->type === 'image' && $item->data->upload) {
                $html['image'] = [
                    'src' => FileTool::thumb($item->path),
                    'width' => $item->data->width,
                    'height' => $item->data->height,
                    'style' => 'border-radius: 3px;margin-bottom: 10px;'
                ];
            }
            if ($item->type === 'video' && $item->data->upload) {
                $html['video'] = [
                    'src' => FileTool::video($item->path),
                    'width' => $item->data->width,
                    'height' => $item->data->height,
                    'duration' => $item->data->duration,
                    'size' => $item->data->size,
                    'pic' => $item->data->pic,
                    'style' => ''
                ];
            }
            $text = $Parsedown->setUrlsLinked(false)->text($item->text);
            $text = str_replace("<p>", "<p class='item-p'>", $text);
            $text = str_replace("<h1>", "<h1 class='item-h1'>", $text);
            $text = str_replace("<h2>", "<h2 class='item-h2'>", $text);
            $text = str_replace("<h3>", "<h3 class='item-h3'>", $text);
            $text = str_replace("<h4>", "<h4 class='item-h4'>", $text);
            $text = str_replace("<h5>", "<h5 class='item-h5'>", $text);
            $text = str_replace("<h6>", "<h6 class='item-h6'>", $text);
            $html['text'] = $text;
            return $html;
        })->all();
        return $nodes;
    }

    public static function getNodeImages($article)
    {
        $content = self::getContent($article);
        $images = collect($content)->filter(function ($item) {
            return $item->type === 'image' && $item->data->upload;
        })->map(function ($item) {
            if ($item->type === 'image' && $item->data->upload) {
                return FileTool::thumb($item->path);
            }
        })->all();

        $arr = [];
        foreach ($images as $image) {
            $arr[] = $image;
        }

        return $arr;
    }


    public
    static function getCover($article)
    {
        $cover = $article->cover;
        if (!empty($cover)) {
            return [
                'path' => $cover,
                'cover_url' => FileTool::thumb($cover),
                'small_url' => FileTool::thumb($cover, FileTool::getImageView(300, 400)),
                'big_url' => FileTool::thumb($cover, FileTool::getImageView(600, 800)),
                'avatar_url' => FileTool::thumb($cover, FileTool::getImageView(120, 120)),
                'share_url' => FileTool::thumb($cover, FileTool::getImageView(400, 320)),
            ];
        } else {
            return [];
        }

    }

    public
    function getCoverAttribute($key)
    {
        $cover = "common/cover.png";

        return empty($key) ? $cover : $key;
    }

    public static function getShareData($article, $user)
    {
        if ($article->status != 1) {
            //return [];
        }
        $cover = $article->cover;
        $tg_id = $user ? hashid_encode($user->id) : 0;
        if ($article->doc->doc_type == 0 || $article->is_try == 1) {
            return [
                'image' => FileTool::thumb($cover, FileTool::getImageView(400, 320)),
                'title' => $article->title,
                'xcx_url' => '/pages/details/article-node-page/main?id=' . hashid_encode($article->id) . '&share=' . $tg_id,
                'qr_code' => urlencode(config('wx.web_host') . 'share-card?type=article&id=' . hashid_encode($article->id)),
                'report' => urlencode(config('wx.web_host') . 'report?type=article&id=' . hashid_encode($article->id)),
            ];
        } else {
            return Doc::getShareData($article->doc, $user);
        }


    }
}
